<script setup>
import { fetchNowDate, fetchAirDate, fetchIndicesDate } from '../api/weatherAPI'
import { computed, ref, watch } from 'vue'
const nowDate = ref({})
const updateTime = ref('')
const airDate = ref({})
const indicesDate = ref({})
import dayjs from 'dayjs'
//引入和风天气图标
import 'qweather-icons/font/qweather-icons.css'
import { useCityStore } from '../stores/cityStore'
//引入和风天气图标
const cityStore = useCityStore()
watch(
  () => cityStore.location,
  () => {
    // city = ref(cityStore.location.name)
    getDate(cityStore.location.id)
  },
  { immediate: true }
)
// getDate(cityStore.location.id)
async function getDate(locationID) {
  const res = await fetchNowDate(locationID)
  console.log(res)
  nowDate.value = res.now
  updateTime.value = dayjs(res.updateTime).format('YYYY年MM月DD日 HH时mm分')
  const resAir = await fetchAirDate(locationID)
  console.log(resAir)
  airDate.value = resAir.now
  const resIndices = await fetchIndicesDate(locationID, 5)
  console.log(resIndices)
  indicesDate.value = resIndices.daily[0]
}
const airText = computed(() => {
  const aqi = airDate.value.aqi
  if (aqi <= 50) {
    return '优'
  } else if (aqi <= 100) {
    return '良'
  } else if (aqi <= 150) {
    return '轻度污染'
  } else if (aqi <= 200) {
    return '中度污染'
  } else if (aqi <= 300) {
    return '重度污染'
  } else if (aqi <= 500) {
    return '严重污染'
  } else {
    return '爆表'
  }
})
</script>
<template>
  <div class="now">
    <div class="top">
      <span>{{ updateTime }}</span>
      <span>{{ cityStore.location.name }}</span>
    </div>
    <div class="mid">
      <div class="air">
        <p>AQI {{ airText }}</p>
      </div>
      <div class="weather">
        <i :class="`qi-${nowDate.icon}`"></i>
        <div>
          <p>{{ nowDate.temp }}°</p>
          <p>{{ nowDate.text }}</p>
        </div>
      </div>
      <div class="text">
        <p>{{ indicesDate.text }}</p>
      </div>
    </div>
    <div class="bottom">
      <div>
        <p>{{ nowDate.windScale }}级</p>
        <p>{{ nowDate.windDir }}</p>
      </div>
      <div>
        <p>{{ nowDate.humidity }}%</p>
        <p>相对湿度</p>
      </div>
      <div>
        <p>{{ indicesDate.category }}</p>
        <p>紫外线</p>
      </div>
      <div>
        <p>{{ nowDate.feelsLike }}°</p>
        <p>体感温度</p>
      </div>
      <div>
        <p>{{ nowDate.vis }}km</p>
        <p>能见度</p>
      </div>
      <div>
        <p>{{ nowDate.precip }}mm</p>
        <p>降水量</p>
      </div>
      <div>
        <p>{{ nowDate.pressure }}hPa</p>
        <p>大气压</p>
      </div>
    </div>
  </div>
</template>
<style scoped>
.now {
  width: 85%;
  background-color: antiquewhite;
  border-radius: 1rem;
  padding: 1rem 0;
  margin: 2rem auto;
}
.now .top {
  padding: 1rem;
  font-size: 2rem;
  overflow: hidden;
  padding-bottom: 0;
  margin-top: -1rem;
}
.now .top > span:nth-of-type(2) {
  font-size: 3rem;
  margin-left: 2rem;
}
.now .top > span:nth-of-type(1) {
  float: right;
  font-size: 2rem;
}
.weather {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: -4rem;
}
.weather > i {
  font-size: 7rem;
  margin-right: 1rem;
}
.text {
  margin: 0 auto;
  font-size: 1.2rem;
  width: 50%;
  margin-top: -1rem;
}
.weather > div > p:nth-of-type(1) {
  font-size: 3rem;
}
.weather > div > p:nth-of-type(2) {
  font-size: 2rem;
}
.bottom {
  width: 96%;
  margin: 0 auto;
  background-color: #dcdfe9;
  border-radius: 1rem;
  padding: 1rem;
  display: flex;
  padding: 0 1rem;
  font-size: 1.8rem;
  justify-content: space-between;
}
.air {
  padding-left: 1rem;
  padding-right: 1rem;
  margin-top: -1.5rem;
  padding-bottom: -3rem;
  overflow: hidden;
}
.air > p {
  text-align: center;
  width: 6rem;
  height: 2rem;
  line-height: 2rem;
  background-color: #a9a538;
  border-radius: 1rem;
  color: white;
  float: right;
}
</style>